<template>
  <div id="app">
    <div id="chart-container" ref="chartContainer"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts';
import {selectGfxmfxList} from "@/api/sjfx/gfxmfx/gfxmfx";
export default {
  name: "Gfxmfx",
  created() {
    this.getGfXm()
  },
  mounted() {
    // this.initChart();
    window.addEventListener('resize', this.resizeChart);
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.resizeChart);
  },
  methods: {
    getGfXm() {
      //查询人员分类的人数
      selectGfxmfxList().then(res => {
        this.initBT(res.data)
      })
    },
    //实例化饼状图
    initBT(data) {
      const chartContainer = this.$refs.chartContainer;
      this.myChart = echarts.init(chartContainer, null, {
        renderer: 'canvas',
        useDirtyRect: false
      });

      const posList = [
        'left', 'right', 'top', 'bottom', 'inside', 'insideTop', 'insideLeft', 'insideRight',
        'insideBottom', 'insideTopLeft', 'insideTopRight', 'insideBottomLeft', 'insideBottomRight'
      ];

      const app = this.app = {
        configParameters: {
          rotate: { min: -90, max: 90 },
          align: { options: { left: 'left', center: 'center', right: 'right' } },
          verticalAlign: { options: { top: 'top', middle: 'middle', bottom: 'bottom' } },
          position: { options: posList.reduce((map, pos) => ({ ...map, [pos]: pos }), {}) },
          distance: { min: 0, max: 100 }
        },
        config: {
          rotate: 90,
          align: 'left',
          verticalAlign: 'middle',
          position: 'insideBottom',
          distance: 15,
          onChange: () => {
            const labelOption = {
              rotate: app.config.rotate,
              align: app.config.align,
              verticalAlign: app.config.verticalAlign,
              position: app.config.position,
              distance: app.config.distance
            };
            this.myChart.setOption({
              series: Array(4).fill({ label: labelOption })
            });
          }
        }
      };

      const labelOption = {
        show: true,
        position: app.config.position,
        distance: app.config.distance,
        align: app.config.align,
        verticalAlign: app.config.verticalAlign,
        rotate: app.config.rotate,
        // 柱状图表内名字
        // formatter: '{c}  {name|{a}}',
        fontSize: 0,
        rich: { name: {} }
      };

      const option = {
        // tooltip鼠标经过显示
        tooltip: { trigger: 'axis', axisPointer: {
            type: 'line',
            lineStyle: {
              color: '#000',
              width: 2,
              type: 'solid'
            }
          } },
        legend: { data: ['电建工法','电力建设协会工法','国家级工法','企业工法','四川省工法','水利建设协会工法','河南省工法'] },
        toolbox: {
          show: true,
          orient: 'vertical',
          left: 'right',
          top: 'center',
          feature: {
            mark: { show: true },
            dataView: { show: true, readOnly: false },
            magicType: { show: true, type: ['line', 'bar', 'stack'] },
            restore: { show: true },
            saveAsImage: { show: true }
          }
        },
        xAxis: [{ type: 'category', axisTick: { show: false }, data: data.yearsArray }],
        yAxis: [{ type: 'value' }],
        series: [
          {
            name: '电建工法',
            type: 'bar',
            // barGap: '10%',
            // barWidth: '40%',
            label: labelOption,
            emphasis: { focus: 'series' },
            data: data.djGfCountsArray
          },
          {
            name: '电力建设协会工法',
            type: 'bar',
            // barGap: '10%',
            // barWidth: '40%',
            label: labelOption,
            emphasis: { focus: 'series' },
            data: data.djJsXhGfCountsArray
          },
          {
            name: '国家级工法',
            type: 'bar',
            // barGap: '10%',
            // barWidth: '40%',
            label: labelOption,
            emphasis: { focus: 'series' },
            data: data.gjjGfCountsArray
          },
          {
            name: '企业工法',
            type: 'bar',
            // barGap: '10%',
            // barWidth: '40%',
            label: labelOption,
            emphasis: { focus: 'series' },
            data: data.qyGfCountsArray
          },
          {
            name: '四川省工法',
            type: 'bar',
            // barGap: '10%',
            // barWidth: '40%',
            label: labelOption,
            emphasis: { focus: 'series' },
            data: data.scsGfCountsArray
          },
          {
            name: '水利建设协会工法',
            type: 'bar',
            // barGap: '10%',
            // barWidth: '40%',
            label: labelOption,
            emphasis: { focus: 'series' },
            data: data.slJsXhGfCountsArray
          },
          {
            name: '河南省工法',
            type: 'bar',
            // barGap: '10%',
            // barWidth: '40%',
            label: labelOption,
            emphasis: { focus: 'series' },
            data: data.hnsGfCountsArray
          },
        ],
        // 添加 dataZoom 配置
        dataZoom: [
          {
            type: 'slider', // 滚动条类型
            show: true, // 显示滚动条
            xAxisIndex: [0], // 控制第一个 x 轴
            bottom: 20, // 滚动条放置在图表底部，距离底部 20px
            start: 0, // 初始位置
            end: 100 // 初始结束位置
          },
          {
            type: 'inside', // 内部缩放类型
            xAxisIndex: [0], // 控制第一个 x 轴
            start: 0, // 初始位置
            end: 100 // 初始结束位置
          }
        ]
      };
      // 设置图表选项
      if (option && typeof option === 'object') {
        this.myChart.setOption(option);
      }

      app.config.onChange(); // 初始化时调用一次
    },
    resizeChart() {
      this.myChart.resize();
    }
  }
}
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
}
#chart-container {
  position: relative;
  height: 800px;
  overflow: hidden;
}
</style>
